package other;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

import java.security.SecureRandom;
import java.time.LocalDateTime;

/**
 * 生成编码工具类
 *
 * @author superC
 * @version 1.0.0
 * @date 2023/7/21
 */

public class GenerateCodeUtil {

    public static final String TEMP = "TEMP";

    /**
     * 默认生成规则：固定值+时间串（8位）+随机数（3位）
     *
     * @return {@link String}
     * @date 2023/7/21 17:54
     * @author superC
     */
    public static String code() {
        return TEMP + GenerateCodeUtil.nowTime() + RandomUtil.randomInt(999);
    }

    /**
     * 当前时间，格式 yyyyMMddHHmmss
     *
     * @return {@link String}
     * @date 2023/7/21 18:05
     * @author superC
     */
    public static String nowTime() {
        return DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss");
    }

    /**
     * 随机生成6位数字字符串
     */
    public static String generateRandomNumber6() {
        int length = 6;
        final String CHARACTERS = "123456789";
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        return sb.toString();
    }

}
